package io.gitee.shengzheng1999.esbcommon.common.utils;

import java.util.regex.Pattern;

import io.gitee.shengzheng1999.esbcommon.exception.BaseException;
import io.gitee.shengzheng1999.esbcommon.exception.ExceptionCodes;

/**
 * @className		: ValidateUtil
 * @description	: 数据有效性检查工具类
 * @summary		:
 * @history		:
 * ------------------------------------------------------------------------------
 * date			version		modifier		remarks                   
 * ------------------------------------------------------------------------------
 * 2023/06/16	1.0.0		sheng.zheng		初版
 *
 */
public class ValidateUtil {
	
	/**
	 * 
	 * @methodName		: checkPhoneNumber
	 * @description	: 检查手机号码格式
	 * @param phoneNumber:手机号码
	 * @return		: 格式正确返回true，否则为false
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2023/06/16	1.0.0		sheng.zheng		初版
	 *
	 */
	public static boolean checkPhoneNumber(String phoneNumber) {
		String pattern = "^1[3-9][0-9]\\d{8}$";
		boolean bRet = Pattern.matches(pattern,phoneNumber);
		return bRet;		
	}
	
	/**
	 * 
	 * @methodName		: phoneNumberValidator
	 * @description	: 手机号码格式校验，格式错误抛出异常
	 * @param propName	: 手机号码的提示名称
	 * @param phoneNumber: 手机号码
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2023/06/16	1.0.0		sheng.zheng		初版
	 *
	 */
	public static void phoneNumberValidator(String propName,String phoneNumber) {
		boolean bRet = checkPhoneNumber(phoneNumber);
		if (!bRet) {
			throw new BaseException(ExceptionCodes.DATA_FORMAT_WRONG,propName + ":"+phoneNumber);
		}
	}
	
	/**
	 * 
	 * @methodName		: checkEmail
	 * @description	: 检查email格式是否正确
	 * @param email		: email邮箱地址
	 * @return		: 格式正确返回true，否则为false
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2023/06/16	1.0.0		sheng.zheng		初版
	 *
	 */
	public static boolean checkEmail(String email) {
		String pattern = "^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w+)+)$";
		boolean bRet = Pattern.matches(pattern,email);
		return bRet;		
	}	
	
	/**
	 * 
	 * @methodName		: emailValidator
	 * @description	: email格式校验，格式错误抛出异常
	 * @param propName	: email的提示名称
	 * @param email		: email邮箱地址
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2023/06/16	1.0.0		sheng.zheng		初版
	 *
	 */
	public static void emailValidator(String propName,String email) {
		boolean bRet = checkEmail(email);
		if (!bRet) {
			throw new BaseException(ExceptionCodes.DATA_FORMAT_WRONG, propName + ":" + email);
		}
	}	
	
	/**
	 * 
	 * @methodName		: checkLoginName
	 * @description	: 检查登录名格式是否正确，
	 * 	格式：字母开头，可以支持字母、数字、以及"_.-@#%"6个特殊符号
	 * @param loginName	: 登录名
	 * @return		: 
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2023/06/16	1.0.0		sheng.zheng		初版
	 *
	 */
	public static boolean checkLoginName(String loginName) {
		String pattern = "^[a-zA-Z]([a-zA-Z0-9_.\\-@#%]*)$";
		boolean bRet = Pattern.matches(pattern,loginName);
		return bRet;
	}
	
	/**
	 * 
	 * @methodName		: loginNameValidator
	 * @description	: 登录名称格式校验，格式错误抛出异常
	 * @param propName	: 登录名称的提示名称
	 * @param loginName	: 登录名称
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2023/06/16	1.0.0		sheng.zheng		初版
	 *
	 */
	public static void loginNameValidator(String propName,String loginName) {
		boolean bRet = checkLoginName(loginName);
		if (!bRet) {
			throw new BaseException(ExceptionCodes.DATA_FORMAT_WRONG, propName + ":" + loginName);
		}
	}	
	
}
